'''
Created on 2012-4-12下午8:24:19

@author: yasenagat
@email:   yasenagat@gmail.com

'''

__doc__ = '''接收文件的服务端'''



from socket import *
import struct
 
ADDR = ('localhost',8000)
BUFSIZE = 1024
FILEINFO_SIZE=struct.calcsize('128s32sI8s')
 
recvSock = socket(AF_INET,SOCK_STREAM)
recvSock.bind(ADDR)
recvSock.listen(True)
 
conn,addr = recvSock.accept()
 
fhead = conn.recv(FILEINFO_SIZE)
filename,temp1,filesize,temp2=struct.unpack('128s32sI8s',fhead)
#print filename,temp1,filesize,temp2
 
filename = 'new_'+filename.strip('\00') #...
fp = open(filename,'wb')
restsize = filesize
while 1:
    if restsize > BUFSIZE:
        filedata = conn.recv(BUFSIZE)
    else:
        filedata = conn.recv(restsize)
    if not filedata: break
    fp.write(filedata)
    restsize = restsize-len(filedata)
    if restsize == 0: break
fp.close()
 
conn.close()
recvSock.close()
 
    